package cn.kgc.controller;

import cn.kgc.pojo.Dep;

import cn.kgc.service.DepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.Console;
import cn.kgc.pojo.Employee;
import cn.kgc.service.DepService;
import com.alibaba.druid.support.json.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**

 * @author 王作栋
 * @version 1.0
 * @date 2022/10/7 - 17:08cmd

 */
@Controller
public class DepController {
    @Autowired
    private DepService depService;



    @RequestMapping("/dep")
    public String deo(){
        return "Dep";
    }

    @GetMapping("/getDepList")
    @ResponseBody
    public Map<String,Object> getDepList(Dep dep,String depName,String limit, String page,Integer deptCount) {
        int crrPage = 1;
        if (page!=null && !"".equals(page)){
            crrPage = Integer.parseInt(page);
        }
        int i = Integer.parseInt(limit);
        crrPage = (crrPage-1)*i;

        HashMap<String,Object> map=new HashMap<>();


        //查询所有部门
        List<Dep> allDep = depService.getDepList(depName,crrPage,i,deptCount);

        //得到总页数
        int totalPage = depService.getTotalRow(depName);
        map.put("count",totalPage);
        map.put("code",0);
        map.put("msg","");
        map.put("data",allDep );


        return map;

    }
    @PostMapping("/addDep")
    public String addDep(Dep dep){
        //新增

        depService.addDep(dep);
        return "redirect:/getDepList";
    }

    @RequestMapping("/updateDepByName")
    public String updateDepByName(Dep dep){
        //修改
        depService.updateDepByName(dep);
        return "redirect:/getDepList";
    }

    //删除部门
    @GetMapping("/deleteDepById")
    public String deleteDepById(Dep dep){

        //删除
        depService.deleteDepById(dep);
        System.out.println(dep);
        return "redirect:/getDepList";
    }




    //查询所有部门绑定员工表下拉列表
    @RequestMapping("/depList")
    @ResponseBody
    public Object depList(){

        Map<String,Object> maps=new HashMap<>();

        List<Dep> depList = depService.findDepList();
        maps.put("data",depList);
        return maps;


    }
    //柱状图
    @GetMapping("/chartzhu")
    @ResponseBody
    public  List<Dep> chartzhu(){
        List<Dep> allDep = depService.chartzhu();
        HashMap<String,Object> map=new HashMap<>();
        map.put("allDep",allDep);
        System.out.println(allDep);
        System.out.println("===========");
        System.out.println(map);
        return allDep;
    }
}
